<?php
namespace App\Services;

use App\Enum\ActivityEnum;
use App\Models\Activity;
use App\Models\UserCoupon;

class PassengerActivityService extends BaseService
{


    /**
     * 发放用户活动奖励
     * @param $user_id
     * @param $activity_id
     * @param int $company_id
     * @return bool
     */
    public static function grantActivityCoupon($user_id, $activity_id ,$company_id = 1)
    {

        $activity = Activity::query()->where('status', 1)->find($activity_id);

        if( !$activity || count($activity->coupon) ==0 )
            return true;

        $insert_data = [];
        $time = date('Y-m-d H:i:s', time());
        foreach ($activity->coupon as $coupon) {
            $insert_data[] = [
                'user_id' => $user_id,
                'company_id' => $company_id,
                'activity_id' => $activity_id,
                'coupon_id' => $coupon->id,
                'from'=> ActivityEnum::FromReceive,
                'status'=> 0,
                'created_at' => $time,
                'updated_at' => $time,
            ];
        }

        return UserCoupon::query()->insert($insert_data);

    }


    /**
     * 验证用户是否已参与某活动
     * @param $user_id
     * @param $activity_id
     * @return bool
     */
    public static function checkGrantActivityCoupon($user_id, $activity_id) :bool
    {

        return UserCoupon::query()->withTrashed()
            ->where('user_id', $user_id)
            ->where('activity_id', $activity_id)
            ->exists();

    }


}
